package com.example.springblog.service;

import com.example.springblog.mapper.BlogMapper;
import com.example.springblog.mapper.UserInfoMapper;
import com.example.springblog.model.BlogInfo;
import com.example.springblog.model.UserInfo;
import com.example.springblog.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogMapper blogMapper;
    public UserInfo selectByName(String username) {
       return userInfoMapper.selectByName(username);
    }

    public UserInfo selectById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo=blogMapper.selectById(blogId);
        if(blogInfo==null&&blogInfo.getUserId()<0){
            log.error("图书不存在或图书作者信息不合法, blogId:{}", blogId);
            return null;
        }
        Integer UserId=blogInfo.getUserId();
        return userInfoMapper.selectById(UserId);
    }

    public Boolean register(String username, String password, String githubUrl) {
        UserInfo userInfo=new UserInfo();
        userInfo.setUserName(username);
        userInfo.setGithubUrl(githubUrl);
        userInfo.setPassword(SecurityUtils.encrypt(password));
        return userInfoMapper.adduser(userInfo);
    }
}
